package com.xyy.yueshijia.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;


import com.bumptech.glide.Glide;
import com.google.gson.Gson;


import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.ProgressStyle;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import com.xyy.yueshijia.GoodsUserActivity;
import com.xyy.yueshijia.R;
import com.xyy.yueshijia.bean.Goods;
import com.xyy.yueshijia.bean.MyListBean;
import com.xyy.yueshijia.bean.MybannerBean;
import com.xyy.yueshijia.bean.MyshouyeBean;
import com.xyy.yueshijia.myadapter.MyAdapter;
import com.xyy.yueshijia.myadapter.MyAdapterSy;
import com.xyy.yueshijia.myadapter.MyListaAdapter;
import com.xyy.yueshijia.myutil.GlideImageLoader;
import com.xyy.yueshijia.myutil.HttpResponse;
import com.xyy.yueshijia.myutil.MyHttpUtils;
import com.xyy.yueshijia.myutil.NetConfig;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerClickListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created by Asus on 2017/2/7.
 */

public class HomePageFragment extends Fragment {
    private  List<MyshouyeBean>list;
    private Button button;
    private SuperRecyclerView superRecyclerView;
    private ImageView imageView;
    private  ImageView imageView2;
    private RecyclerView recyclerview;
    private RecyclerView recyclerView2;
    private  RecyclerView recyclerView3;
    private int page=1;
    private List<Object> lists;
    private List<String> listss;
    private List<MyshouyeBean>myshouyeBeanList;
    private  List<Goods>goodsList;
    private Banner banner;
     private List<MyListBean>mylist;
    private MyListaAdapter adapter;
    private MyAdapter adapter2;
    private MyAdapterSy adapter3;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         View view = LayoutInflater.from(getActivity()).inflate(R.layout.homepagefragment, container, false);
         View vhead1=LayoutInflater.from(getActivity()).inflate(R.layout.homehand1,container,false);
         View vhead2=LayoutInflater.from(getActivity()).inflate(R.layout.homehead2,container,false);
         View vhead3=LayoutInflater.from(getActivity()).inflate(R.layout.homehead3,container,false);
         initlist();
        button= (Button) view.findViewById(R.id.shousuobtn);
        imageView2= (ImageView) vhead3.findViewById(R.id.homeimage2);
        imageView= (ImageView) vhead2.findViewById(R.id.homeimage);
        //这是标题下面的东西

        recyclerview=(RecyclerView) vhead1.findViewById(R.id.homerecyclerview);

        recyclerView2=(RecyclerView) vhead2.findViewById(R.id.homerecyclerview2);

        recyclerView3=(RecyclerView)vhead3.findViewById(R.id.homerecyclerview3);

        superRecyclerView= (SuperRecyclerView) view.findViewById(R.id.sreccclerview);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        superRecyclerView.setLayoutManager(layoutManager);

        superRecyclerView.setRefreshEnabled(true);//可以定制是否开启下拉刷新
        superRecyclerView.setLoadMoreEnabled(true);//可以定制是否开启加载更多
        superRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);//可以自定义下拉刷新的样式
        superRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);//可以自定义上拉加载的样式
       // superRecyclerView.setArrowImageView(R.mipmap.iconfont_downgrey);//设置下拉箭头





        adapter3=new MyAdapterSy(getContext(),list);
        superRecyclerView.setAdapter(adapter3);

         adapter3.addHeaderView(vhead1);

         adapter3.addHeaderView(vhead2);
      //  adapter3.addHeaderView(vhead3);
        adapter3.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                Toast.makeText(getContext(),"you click item"+position,Toast.LENGTH_SHORT).show();
            }
        });
        adapter3.setOnItemChildClickListener(new SuperBaseAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(SuperBaseAdapter adapter, View view, int position) {
                if(view.getId() == R.id.homezhutitle){
                    Toast.makeText(getContext(),"you item child click"+goodsList.get(position).getGoods_name(),Toast.LENGTH_SHORT).show();
                }
            }
        });

      //  xRecyclerView.addHeaderView(vhead1);
      //  xRecyclerView.addHeaderView(vhead2);
       // xRecyclerView.addHeaderView(vhead3);
      //  adapter3=new MyAdapterSy(getContext(),list);
      //  xRecyclerView.setAdapter(adapter3);
        //刷新样式
       // xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        //xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.SquareSpin);

        //自动加载
        final View finalView = view;
        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //在布局加载完成之后，才能执行，不然看不到效果
                loadbanner();
                loadlist();
                loadlist2();
             //   loadlist3();
                loaddata();
                finalView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        });

      superRecyclerView.setLoadingListener(new SuperRecyclerView.LoadingListener() {
          @Override
          public void onRefresh() {
               loadbanner();
               loadlist();
                loadlist2();
               //  loadlist3();
                 loaddata();
              superRecyclerView.completeRefresh();
          }
          @Override
          public void onLoadMore() {
              adddata();
              superRecyclerView.setNoMore(true);
          }
      });

        //下拉刷新  和上拉加载
      //  xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
         //   @Override
         //   public void onRefresh() {
               // loadbanner();
              //  loadlist();
              //  loadlist2();
             //   loadlist3();
             //   loaddata();
//                必须加上这句话
           //     xRecyclerView.refreshComplete();
          //  }

         //   @Override
          //  public void onLoadMore() {
             //   adddata();
//                此处为大坑必须注意一下
//                必须加上这句话
            //    xRecyclerView.loadMoreComplete();
          //  }
      //  });
        LinearLayoutManager  manager2=new LinearLayoutManager(getActivity());
        manager2.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerview.setLayoutManager(manager2);
        adapter = new MyListaAdapter(getContext(),mylist);
        recyclerview.setAdapter(adapter);

        //recyclerview 点击事件
        adapter.setOnItemClickListener(new MyListaAdapter.OnRecyclerViewItemClickListener(){
            @Override
            public void onItemClick(View view , String data){
                  Intent intent=new Intent();


                Toast.makeText(getContext(), data, Toast.LENGTH_SHORT).show();
            }
        });

        LinearLayoutManager  manager=new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView2.setLayoutManager(manager);


        LinearLayoutManager  manager3=new LinearLayoutManager(getActivity());
        manager3.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView3.setLayoutManager(manager3);

        //这是标题框的 东西


        banner = (Banner)vhead1.findViewById(R.id.banner);
        banner.setOnBannerClickListener(new OnBannerClickListener() {
            @Override
            public void OnBannerClick(int position) {
                Toast.makeText(getContext(),"我被点击了",Toast.LENGTH_SHORT).show();
            }
        });

         button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent intent=new Intent(getContext(), GoodsUserActivity.class);
                  startActivity(intent);
             }
         });
        return view;
    }

    private void initlist() {
        lists=new ArrayList<>();
        listss=new ArrayList<>();
        mylist=new ArrayList<>();
        myshouyeBeanList=new ArrayList<>();
        goodsList=new ArrayList<>();
        list=new ArrayList<>();
    }
    private void loadbanner() {
        HashMap<String, String> params = new HashMap<>();
        params.put("act", "app");
        params.put("op", "index1");
        params.put("v", "2.1.2");
        params.put("req_time", "1487053101071");
        params.put("client", "android");
        params.put("channel", "yingyongbao");
        params.put("curpage", page + "");
        params.put("token", "749a036dc06ae8b3a120848995a9f306");
        params.put("key", "aa1fb8a20d852b140e4b8089170a509f");
        Log.i("ooooooooo","xccccc"+params);
        MyHttpUtils.getInstance()
                .get(NetConfig.BASE_URL,"", params, new HttpResponse() {
                    @Override
                    public void onResponse(String data) {
                        Log.i("vvvvvvvvvvvvv","vvvvvvvvvvvvv"+data);
                        try {
                            JSONObject object = new JSONObject(data);

                            JSONObject object1 = object.getJSONObject("datas");
                            JSONArray array=object1.getJSONArray("banner");
                            // lists.clear();
                            Gson gson = new Gson();
                            for (int i = 0, size = array.length(); i < size; i++) {
                                String string = array.getString(i);
                                List<MybannerBean>  mybannerBeen=new ArrayList<MybannerBean>();
                                mybannerBeen.add(gson.fromJson(string,MybannerBean.class));
                                Log.i("oooooxxxxxxxxxcc","mmmmmmmmmmmmmmmm"+string);
                                for (int k=0;k<mybannerBeen.size();k++){
                                    lists.add(mybannerBeen.get(k).getAdvertImg());
                                    listss.add(mybannerBeen.get(k).getAdvertTitle());
                                }
                                //设置banner样式

                                banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
                                //设置banner动画效果
                                //BackgroundToForeground逐渐放大出来
                                //ZoomOut由内向外
                                //RotateDown扇形
                                //FlipVertical翻页
                               banner.setBannerAnimation(Transformer.ZoomOut);
                                //设置标题集合（当banner样式有显示title时）
                                banner.setBannerTitles(listss);
                                banner.setImageLoader(new GlideImageLoader());
                                banner.setImages(lists);
                                //设置指示器位置（当banner模式中有指示器时）
                                banner.setIndicatorGravity(BannerConfig.CENTER);
                                banner.start();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(String msg) {
                        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
                    }
                });
    }
    public void loadlist(){
        HashMap<String, String> params = new HashMap<>();
        params.put("act", "app");
        params.put("op", "index1");
        params.put("v", "2.1.2");
        params.put("req_time", "1487053101071");
        params.put("client", "android");
        params.put("channel", "yingyongbao");
        params.put("curpage", page + "");
        params.put("token", "749a036dc06ae8b3a120848995a9f306");
        params.put("key", "aa1fb8a20d852b140e4b8089170a509f");
        MyHttpUtils.getInstance()
                .get(NetConfig.BASE_URL, "", params, new HttpResponse() {
                    @Override
                    public void onResponse(String data) {
                        try {
                            JSONObject object = new JSONObject(data);
                            JSONObject object1 = object.getJSONObject("datas");
                            JSONArray array=object1.getJSONArray("relation_good_four");
                            // listbtn.clear();
                            Gson gson = new Gson();

                            for (int i = 0, size = array.length(); i < size; i++) {
                                String string = array.getString(i);
                                mylist.add(gson.fromJson(string, MyListBean.class));
                                Log.i("xxxxxxxxxxxcc","mmmmmmmmmmmmmmmm"+string);
                                Log.i("bbbbbbbbbbbbb","===="+mylist);
                            }

                            adapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(String msg) {

                    }
                });

    }
     public void loadlist2(){
        HashMap<String, String> params = new HashMap<>();
         params.put("act", "app");
         params.put("op", "index1");
         params.put("v", "2.1.2");
         params.put("req_time", "1487053101071");
         params.put("client", "android");
         params.put("channel", "yingyongbao");
         params.put("curpage", page + "");
         params.put("token", "749a036dc06ae8b3a120848995a9f306");
         params.put("key", "aa1fb8a20d852b140e4b8089170a509f");
        Log.i("ooooooooo","xccccc"+params);
        MyHttpUtils.getInstance()
                .get(NetConfig.BASE_URL, "", params, new HttpResponse() {
                    @Override
                    public void onResponse(String data) {
                        try {
                            JSONObject object = new JSONObject(data);
                            JSONObject object1 = object.getJSONObject("datas");
                            JSONArray array=object1.getJSONArray("data_type");
                            // listbtn.clear();
                            Gson gson = new Gson();

                            for (int i = 0, size = array.length(); i < size; i++) {
                                String string = array.getString(i);
                                myshouyeBeanList.add(gson.fromJson(string, MyshouyeBean.class));


                                Log.i("xxxxxxxxxxxcc", "mmmmmmmmmmmmmmmm" + string);
                                Log.i("wwwbbbbbbbbbbbbb", "====" + myshouyeBeanList);

                            }
                            String imageURI = myshouyeBeanList.get(0).getRelation_object_image();
                            Glide.with(getActivity())
                                    .load(imageURI)
                                    .placeholder(R.drawable.bg_morentu)
                                    // .bitmapTransform(transformation) //转换
                                    .error(R.drawable.bg_morentu)
                                    .into(imageView);
                            goodsList = myshouyeBeanList.get(0).getGoods_special_list();
                            Log.i("ppgggggggrr","+++++++++++=="+goodsList.size());
                            Log.i("gggggggggrr","++++++++++++"+goodsList);

                            adapter2 = new MyAdapter(getActivity(),goodsList);
                             Log.i("ippppppippp","==========="+goodsList);
                            recyclerView2.setAdapter(adapter2);

                          //  recyclerview2 点击事件
                            adapter2.setOnItemClickListener(new MyAdapter.OnRecyclerViewItemClickListener(){
                                @Override
                                public void onItemClick(View view , String data){
                                    Toast.makeText(getContext(), data, Toast.LENGTH_SHORT).show();
                                }
                            });
                            adapter2.notifyDataSetChanged();
                            Log.i("jjjjjjjjjjjjjjjj0", "mmmmmmmmmmmmmmmm" + goodsList.toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(String msg) {

                    }
                });

    }
    public void loadlist3(){
        HashMap<String, String> params = new HashMap<>();
        params.put("act", "app");
        params.put("op", "index1");
        params.put("v", "2.1.2");
        params.put("req_time", "1487053101071");
        params.put("client", "android");
        params.put("channel", "yingyongbao");
        params.put("curpage", page + "");
        params.put("token", "749a036dc06ae8b3a120848995a9f306");
        params.put("key", "aa1fb8a20d852b140e4b8089170a509f");
        Log.i("ooooooooo","xccccc"+params);
        MyHttpUtils.getInstance()
                .get(NetConfig.BASE_URL, "", params, new HttpResponse() {
                    @Override
                    public void onResponse(String data) {
                        try {
                            JSONObject object = new JSONObject(data);
                            JSONObject object1 = object.getJSONObject("datas");
                            JSONArray array=object1.getJSONArray("data_type");
                            // listbtn.clear();
                            Gson gson = new Gson();

                            for (int i = 0, size = array.length(); i < size; i++) {
                                String string = array.getString(i);
                                myshouyeBeanList.add(gson.fromJson(string, MyshouyeBean.class));


                                Log.i("cccccxxxxxxxcc", "mmmmmmmmmmmmmmmm" + string);
                                Log.i("kkkkkkbbbbbbbbbbbbb", "====" + myshouyeBeanList);

                            }
                            String imageURI = myshouyeBeanList.get(1).getRelation_object_image();
                            Glide.with(getActivity())
                                    .load(imageURI)
                                    .placeholder(R.drawable.bg_morentu)
                                    // .bitmapTransform(transformation) //转换
                                    .error(R.drawable.bg_morentu)
                                    .into(imageView2);
                                  goodsList = myshouyeBeanList.get(1).getGoods_special_list();

                            Log.i("xxxxggggggrr","++++++++++++"+goodsList);
                            adapter2 = new MyAdapter(getActivity(),goodsList);
                            recyclerView3.setAdapter(adapter2);
                            //recyclerview2 点击事件
                          //  adapter2.setOnItemClickListener(new MyAdapter.OnRecyclerViewItemClickListener(){
                              //  @Override
                              //  public void onItemClick(View view , String data){
                             //       Toast.makeText(getContext(), data, Toast.LENGTH_SHORT).show();
                           //     }
                       //     });
                            adapter2.notifyDataSetChanged();
                            Log.i("jjjjjjjjjjjjjjjj0", "mmmmmmmmmmmmmmmm" + goodsList.toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(String msg) {

                    }
                });

    }
    public void loaddata(){
        page=2;
        HashMap<String, String> params = new HashMap<>();
        params.put("act", "app");
        params.put("op", "index1");
        params.put("v", "2.1.2");
        params.put("req_time", "1487053101071");
        params.put("client", "android");
        params.put("channel", "yingyongbao");
        params.put("curpage", page + "");
        params.put("token", "749a036dc06ae8b3a120848995a9f306");
        params.put("key", "aa1fb8a20d852b140e4b8089170a509f");
        Log.i("ooooooooo","xccccc"+params);
        MyHttpUtils.getInstance()
                .get(NetConfig.BASE_URL, "", params, new HttpResponse() {
                    @Override
                    public void onResponse(String data) {
                        try {
                            JSONObject object = new JSONObject(data);
                            JSONObject object1 = object.getJSONObject("datas");
                            JSONArray array=object1.getJSONArray("data_type");

                            // listbtn.clear();
                            Gson gson = new Gson();

                            for (int i = 0, size = array.length(); i < size; i++) {
                                String string = array.getString(i);
                                Log.i("ccyyyyyyyyyyyyy", "mmmmmmmmmmmmmmmm" + string);
                                list.add(gson.fromJson(string,MyshouyeBean.class));

                                Log.i("wwwpppppppppp", "====" + list);

                            }
                            adapter3.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(String msg) {

                    }
                });
    }
    public void adddata(){

        HashMap<String, String> params = new HashMap<>();
        params.put("act", "app");
        params.put("op", "index1");
        params.put("v", "2.1.2");
        params.put("req_time", "1487053101071");
        params.put("client", "android");
        params.put("channel", "yingyongbao");
        params.put("curpage", page + "");
        params.put("token", "749a036dc06ae8b3a120848995a9f306");
        params.put("key", "aa1fb8a20d852b140e4b8089170a509f");
        Log.i("ooooooooo","xccccc"+params);
        MyHttpUtils.getInstance()
                .get(NetConfig.BASE_URL, "", params, new HttpResponse() {
                    @Override
                    public void onResponse(String data) {
                        try {
                            JSONObject object = new JSONObject(data);
                            JSONObject object1 = object.getJSONObject("datas");
                            JSONArray array=object1.getJSONArray("data_type");

                            // listbtn.clear();
                            Gson gson = new Gson();

                            for (int i = 0, size = array.length(); i < size; i++) {
                                String string = array.getString(i);
                                Log.i("ccyyyyyyyyyyyyy", "mmmmmmmmmmmmmmmm" + string);
                                list.add(gson.fromJson(string,MyshouyeBean.class));

                                Log.i("wwwpppppppppp", "====" + list);

                            }
                            adapter3.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(String msg) {

                    }
                });


    }
}
